package com.cyf.plc.ui.fragment

import android.annotation.SuppressLint
import androidx.navigation.Navigation
import com.cyf.library.util.logi
import com.cyf.plc.R
import com.cyf.plc.base.BaseFragment
import com.cyf.plc.bean.PlcWriteAttribute
import com.cyf.plc.bean.SystemStatus
import com.cyf.plc.constant.Constants
import com.cyf.plc.databinding.FragmentPlcModeBinding
import com.cyf.plc.model.PlcAttribute
import com.cyf.plc.plc.PlcExecutor
import com.cyf.plc.util.viewModelOf
import com.cyf.plc.viewmodel.PlcViewModel
import kotlinx.android.synthetic.main.fragment_plc_mode.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@ExperimentalCoroutinesApi
class PlcModeFragment : BaseFragment<PlcViewModel, FragmentPlcModeBinding>() {

    override fun createViewModel(): PlcViewModel = viewModelOf(mViewModelProvider)

    private var systemStatus: SystemStatus? = null

    override fun initView() {
        this.systemStatus = arguments?.getParcelable(Constants.SYSTEM_STATUS)
        "获取到系统状态:$systemStatus".logi()
        //初始化系统状态
        if (systemStatus != null)
            initSystemStatus(systemStatus)
        //制冷
        tv_Refrigeration.setOnClickListener {
            if (systemStatus != null)
                PlcExecutor.write(
                    PlcWriteAttribute(
                        systemStatus!!.modePlcAttribute.offSet,
                        systemStatus!!.modeColdAttribute.attributeValue,
                        systemStatus!!.modePlcAttribute.addressUnit
                    )
                )
        }
        //制热
        tv_Heating.setOnClickListener {
            if (systemStatus != null)
                PlcExecutor.write(
                    PlcWriteAttribute(
                        systemStatus!!.modePlcAttribute.offSet,
                        systemStatus!!.modeHotAttribute.attributeValue,
                        systemStatus!!.modePlcAttribute.addressUnit
                    )
                )
        }
        //除湿
        tv_Dehumidification.setOnClickListener {
            if (systemStatus != null)
                PlcExecutor.write(
                    PlcWriteAttribute(
                        systemStatus!!.modePlcAttribute.offSet,
                        systemStatus!!.modeDehumidifyAttribute.attributeValue,
                        systemStatus!!.modePlcAttribute.addressUnit
                    )
                )
        }
        //通风
        tv_ventilation.setOnClickListener {
            if (systemStatus != null)
                PlcExecutor.write(
                    PlcWriteAttribute(
                        systemStatus!!.modePlcAttribute.offSet,
                        systemStatus!!.modeVentilationAttribute.attributeValue,
                        systemStatus!!.modePlcAttribute.addressUnit
                    )
                )
        }
        //返回上级界面
        iv_back.setOnClickListener {
//            activity?.onBackPressed()
            Navigation.findNavController(it).popBackStack()
        }
    }

    private fun initSystemStatus(systemStatus: SystemStatus?) {
        if (systemStatus != null) {
            tv_Refrigeration.setCompoundDrawablesWithIntrinsicBounds(
                null,
                if (systemStatus.modeValue == systemStatus.modeColdAttribute.attributeValue) this.resources.getDrawable(
                    R.drawable.icon_refrigeration_on
                ) else this.resources.getDrawable(R.drawable.icon_refrigeration_off),
                null,
                null
            )

            tv_ventilation.setCompoundDrawablesWithIntrinsicBounds(
                null,
                if (systemStatus.modeValue == systemStatus.modeVentilationAttribute.attributeValue) this.resources.getDrawable(
                    R.drawable.icon_ventilation_on
                ) else this.resources.getDrawable(R.drawable.icon_ventilation_off),
                null,
                null
            )

            tv_Dehumidification.setCompoundDrawablesWithIntrinsicBounds(
                null,
                if (systemStatus.modeValue == systemStatus.modeDehumidifyAttribute.attributeValue) this.resources.getDrawable(
                    R.drawable.icon_dehumidification_on
                ) else this.resources.getDrawable(R.drawable.icon_dehumidification_off),
                null,
                null
            )

            tv_Heating.setCompoundDrawablesWithIntrinsicBounds(
                null,
                if (systemStatus.modeValue == systemStatus.modeHotAttribute.attributeValue) this.resources.getDrawable(
                    R.drawable.icon_heating_on
                ) else this.resources.getDrawable(R.drawable.icon_heating_off),
                null,
                null
            )
        }
    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun getPlcAttributeEvent(plcAttribute: PlcAttribute) {
        ("获取到Attribute更新:$plcAttribute").logi()
        var shouldUpdate = false
        when (plcAttribute.attributeId) {
            systemStatus!!.modePlcAttribute.attributeId -> {
                shouldUpdate = true
                systemStatus!!.modeValue = plcAttribute.value
            }
        }
        if (shouldUpdate) {
            initSystemStatus(systemStatus)
        }
    }

    override fun layoutId(): Int = R.layout.fragment_plc_mode
}
